import os
import openpyxl
from openpyxl import Workbook

results=[0,0,0,0,0,0,0,0,0,0,0]#标志序号，每个政策的9个分量,和最后的PMG值
resultsword=['政策编号','政策性质','政策时效','政策领域','氢能应用领域','政策内容','政策功能','CCU','政策客体','政策作用范围','PMG平均值']
def ReadXlxs():
    workbook=openpyxl.load_workbook('D:\C++ project\c++\天津地区政策.xlsx')#读入存放政策文件的xlxs
    sheet=workbook['Sheet1']#指定读入的表
    print(sheet.dimensions)#获取表的大小
    #print(sheet)#输出表大小
    cell=sheet['D2:D22']#根据表的大小修改读入列数   手动修改
    #print(cell)
    resultsxls=openpyxl.load_workbook('D:\C++ project\c++\天津地区政策result.xlsx')#存放结果的文件，手动修改
    sheet=resultsxls['Sheet1']#xlsx中的具体表，手动修改
    sheet.append(resultsword)#存放表头目录
    resultsxls.save('D:\C++ project\c++\天津地区政策result.xlsx')#保存

    for(i) in cell:#轮流检验每一个政策内容
        for j in i:
            print(j.value)
            #print(j.value.find("政策"))
            results[0]+=1 #更新序号
            results[1]=Nature(j.value)#为了便于输入，将值依次存入对应的数值位
            results[2]=Times(j.value)
            results[3]=Fields(j.value)
            results[4]=HydrogenApplication(j.value)
            results[5]=Content(j.value)
            results[6]=Funcation(j.value)
            results[7]=CCU(j.value)
            results[8]=Object(j.value)
            results[9]=Range(j.value)

            pmg=0 #清零pmg，即清空上次结果

            for i in results: #计算本次pmg结果
                pmg+=i

            pmg=pmg-results[0]#减去序号部分 相加的时候把序号加入了
            results[10]=pmg 

            resultsxls=openpyxl.load_workbook('D:\C++ project\c++\天津地区政策result.xlsx')#存放文件的地址，手动修改
            sheet=resultsxls['Sheet1']
            sheet.append(results)#存放数据
            resultsxls.save('D:\C++ project\c++\天津地区政策result.xlsx')
            results[1]=0#归零数据
            results[2]=0
            results[3]=0
            results[4]=0
            results[5]=0
            results[6]=0
            results[7]=0
            results[8]=0
            results[9]=0
            results[10]=0
            #print("time is")
            #print(y)
            

def Nature(str):
    #str="啊八八八八预计adadas中长期，adhashu布局开发"
    count=[0,0,0,0]
    yuce=['预计','目标','展望','远期','中长期','短期','预期','推测',]
    jianguan=['跟踪','督促','监管','监督','管理','检测','监控','预警','风险']
    jianyi=['开展','加强','增强','建立','强化','协调','制定','研发','开发','建设']
    yindao=['推动','引导','鼓励','支持','引领','规划','形成','布局','构建','方向']
    number=0
    for i in yuce:
        if str.find(i)!=-1:
            count[0]=1
            break
    for i in jianguan:
        if str.find(i)!=-1:
            count[1]=1
            break
    for i in jianyi:
        if str.find(i)!=-1:
            count[2]=1
            break
    for i in yindao:
        if str.find(i)!=-1:
            count[3]=1
            break

    for j in count:
        print(j)
        number+=j
    return(number*0.25)

def Times(str):
    #str="啊八八八八预计adadas中长期，adhashu布局开发"
    count=[0,0,0]
    duanqi=['2025','短期','5年']
    zhongqi=['2030','2035','中期','10年']
    changqi=['2050','长期','中长期','20年']
    number=0
    for i in duanqi:
        if str.find(i)!=-1:
            count[0]=1
            break
    for i in zhongqi:
        if str.find(i)!=-1:
            count[1]=1
            break
    for i in changqi:
        if str.find(i)!=-1:
            count[2]=1
            break

    for j in count:
        print(j)
        number+=j
    if(number==3):
        return 1
    if(number==2):#因为0.333的倍数不能等于0.667，1，所以采取分支直接返回
        return 0.667
    return(number*0.333)


def Fields(str):
    #str="啊八八八八预计adadas中长期，adhashu布局开发"
    count=[0,0,0,0]
    yuce=['金融','资金','财政','税','投资','商业','融资','经济',]
    jianguan=['制度','人才','高校','社会','宣传','学校']
    jianyi=['技术','装备','研发','创新','科技','科研','工艺']
    yindao=['能源','低碳','环保','环境','气候','绿色','节能']
    number=0
    for i in yuce:
        if str.find(i)!=-1:
            count[0]=1
            break
    for i in jianguan:
        if str.find(i)!=-1:
            count[1]=1
            break
    for i in jianyi:
        if str.find(i)!=-1:
            count[2]=1
            break
    for i in yindao:
        if str.find(i)!=-1:
            count[3]=1
            break

    for j in count:
        print(j)
        number+=j
    return(number*0.25)

def HydrogenApplication(str):
    #str="啊八八八八预计adadas中长期，adhashu布局开发"
    count=[0,0,0,0,0]
    yuce=['新能源汽车','车','交通']
    jianguan=['风电','光伏','电力','发电']
    jianyi=['钢铁','炼铁','冶金','发电']
    yindao=['建材','建筑','土木']
    meitan=['煤','开采','焦炭','焦炉气']
    number=0
    for i in yuce:
        if str.find(i)!=-1:
            count[0]=1
            break
    for i in jianguan:
        if str.find(i)!=-1:
            count[1]=1
            break
    for i in jianyi:
        if str.find(i)!=-1:
            count[2]=1
            break
    for i in yindao:
        if str.find(i)!=-1:
            count[3]=1
            break
    for i in meitan:
        if str.find(i)!=-1:
            count[4]=1
            break

    for j in count:
        print(j)
        number+=j

    return(number*0.2)

def Content(str):
    #str="啊八八八八预计adadas中长期，adhashu布局开发"
    count=[0,0,0,0,0,0,0,0,0,0,0,0]
    yuce=['技术','研发','创新']
    jianguan=['基础工程','基础建设','基础设施']
    jianyi=['示范','试点']
    yindao=['税']
    meitan=['补贴','贷款']
    chanyebiaozhun=['标准','规范']
    rencaipeiyang=['人才','高校']
    guojihezuo=['国际']
    pingtaijianshe=['平台']
    xuanchuanyindao=['宣传','普及']
    anquanguanli=['安全','风险']
    lifabaozhang=['法律','法规']

    number=0
    for i in yuce:
        if str.find(i)!=-1:
            count[0]=1
            break
    for i in jianguan:
        if str.find(i)!=-1:
            count[1]=1
            break
    for i in jianyi:
        if str.find(i)!=-1:
            count[2]=1
            break
    for i in yindao:
        if str.find(i)!=-1:
            count[3]=1
    for i in meitan:
        if str.find(i)!=-1:
            count[4]=1
    for i in chanyebiaozhun:
        if str.find(i)!=-1:
            count[5]=1
            break
    for i in rencaipeiyang:
        if str.find(i)!=-1:
            count[6]=1
            break
    for i in guojihezuo:
        if str.find(i)!=-1:
            count[7]=1
            break
    for i in pingtaijianshe:
        if str.find(i)!=-1:
            count[8]=1
            break
    for i in xuanchuanyindao:
        if str.find(i)!=-1:
            count[9]=1
            break
    for i in anquanguanli:
        if str.find(i)!=-1:
            count[10]=1
            break
    for i in lifabaozhang:
        if str.find(i)!=-1:
            count[11]=1
            break

    for j in count:
        print(j)
        number+=j
    x=0.0833
    if number==12:
        return 1
    return(number*x)

def Funcation(str):
    #str="啊八八八八预计adadas中长期，adhashu布局开发"
    count=[0,0,0,0]
    yuce=['技术','研发','创新','设备','创新','科技','科研','工艺',]
    jianguan=['能源','低碳','环保','环境','气候','绿色','节能']
    jianyi=['引导','鼓励','支持','引领','规划','布局','方向','规范']
    yindao=['新能源','化石能源','新型能源','可再生能源']
    number=0
    for i in yuce:
        if str.find(i)!=-1:
            count[0]=1
            break
    for i in jianguan:
        if str.find(i)!=-1:
            count[1]=1
            break
    for i in jianyi:
        if str.find(i)!=-1:
            count[2]=1
            break
    for i in yindao:
        if str.find(i)!=-1:
            count[3]=1
            break

    for j in count:
        print(j)
        number+=j
    return(number*0.25)

def CCU(str):
    #str="啊八八八八预计adadas中长期，adhashu布局开发"
    count=[0,0]
    yuce=['二氧化碳捕集和利用','碳捕集','碳利用','CCU']
    number=0
    for i in yuce:
        if str.find(i)!=-1:
            count[0]=1
            break

    if count[0]==0:
        count[1]=1

    if count[0]==1:
        return 1 #和CCU协同则直接返回1
    return 0#无则返回0

def Object(str):
    #str="啊八八八八预计adadas中长期，adhashu布局开发"
    count=[0,0,0,0,0]
    yuce=['政府']
    jianguan=['企业']
    jianyi=['金融机构','银行']
    yindao=['群众','公众','公民','人民']
    qitabumen=['高校','院校','创新中心','研究中心','实验室','研究平台','科研院所']
    number=0
    for i in yuce:
        if str.find(i)!=-1:
            count[0]=1
            break
    for i in jianguan:
        if str.find(i)!=-1:
            count[1]=1
            break
    for i in jianyi:
        if str.find(i)!=-1:
            count[2]=1
            break
    for i in yindao:
        if str.find(i)!=-1:
            count[3]=1
            break
    for i in qitabumen:
        if str.find(i)!=-1:
            count[4]=1
            break

    for j in count:
        print(j)
        number+=j

    return(number*0.2)

def Range(str):
    #str="啊八八八八预计adadas中长期，adhashu布局开发"
    count=[0,0,0,0]
    yuce=['制','储','输','用']
    jianguan=['制','储','输','用','运氢']
    jianyi=['加氢']
    number=0
    for i in yuce:
        if str.find(i)!=-1:
            count[0]=1
            count[3]=1
            break
    for i in jianguan:
        if str.find(i)!=-1:
            count[1]=1
            break
    for i in jianyi:
        if str.find(i)!=-1:
            count[2]=1
            break

    for j in count:
        print(j)
        number+=j
    return(number*0.25)

ReadXlxs()